<?php

namespace App\Http\Controllers\Manage;

use App\Http\Model\Manage\AdminRoleModel;
use App\Http\Model\Manage\AdminRuleActionModel;
use App\Http\Model\Manage\AdminRuleModel;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

class RoleController extends BasicController
{

    /**
     * 删除角色
     * @param Request $request
     * @param string $code
     * @return array|string
     */
    public function deleteInfoAction(Request $request, string $code): array|string
    {
        $has = AdminRoleModel::where([
            ['code', '=', $code],
        ])->first();
        if (!$has)
        {
            return errorReturn("角色不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $underRoleCode = $this->getAllRoleUnderCode($role->code);
        if (!in_array($role->code, $underRoleCode))
        {
            return errorReturn("无权限操作该角色");
        }
        $underThisRoleCode = $this->getAllRoleUnderCode($has->code);
        $delete = (new AdminRoleModel)->deleteInfo([], [
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ], "code", $underThisRoleCode);
        if ($delete)
        {
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }

    /**
     * 修改角色状态
     * @param Request $request
     * @param string $code
     * @return array|string
     */
    public function statusInfoAction(Request $request, string $code): array|string
    {
        $has = AdminRoleModel::where([
            ['code', '=', $code],
        ])->first();
        if (!$has)
        {
            return errorReturn("角色不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $underRoleCode = $this->getAllRoleUnderCode($role->code);
        if (!in_array($role->code, $underRoleCode))
        {
            return errorReturn("无权限操作该角色");
        }
        $update = (new AdminRoleModel)->updateInfo(dataToWhere([
            "code"=>$code,
            "version"=>$has->version,
        ]), [
            "status"=>$has->status == 1 ? 0: 1,
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($update)
        {
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }

    /**
     * 添加角色
     * @param Request $request
     * @return View
     */
    public function addInfoAction(Request $request): array|string
    {
        $data = $request->all();
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $ltar001 = $data["ltar001"];
        $has = AdminRoleModel::where([
            ['ltar001', '=', $ltar001],
        ])->first();
        if ($has)
        {
            return errorReturn("角色已存在");
        }
        $roleId = (new AdminRoleModel)->createInfo([
            "ltar001"=>$ltar001,
            "ltar002"=>$data["ltar002"],
            "ltarCode"=>"",
            "ltaraCode"=>"",
            "pCode"=>$data["pCode"],
            "cLtaCode"=>$admin->code,
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($roleId > 0 )
        {
            return successReturn("添加成功");
        }
        else
        {
            return errorReturn("添加失败");
        }

    }

    /**
     * 修改角色信息
     * @param Request $request
     * @return View
     */
    public function editInfoAction(Request $request): array|string
    {
        $data = $request->all();
        $has = AdminRoleModel::where([
            ['code', '=', $data["code"]],
        ])->first();
        if (!$has)
        {
            return errorReturn("角色不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $underRoleCode = $this->getAllRoleUnderCode($role->code);
        if (!in_array($role->code, $underRoleCode))
        {
            return errorReturn("无权限操作该角色");
        }
        $underThisRoleCode = $this->getAllRoleUnderCode($has->code);
        if (in_array($data["pCode"],$underThisRoleCode))
        {
            return errorReturn("不可以将上级角色设置为其下级角色");
        }
        $update = (new AdminRoleModel)->updateInfo(dataToWhere([
            "code"=>$has->code,
            "version"=>$has->version
        ]), [
            "ltar001"=>$data["ltar001"],
            "ltar002"=>$data["ltar002"],
            "pCode"=>$data["pCode"],
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($update)
        {
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }

    /**
     * 角色列表页面
     * @param Request $request
     * @return View
     */
    public function pageTableView(Request $request): View
    {
        $loginInfo = $this->getLoginInfo($request);
        $role = $loginInfo['role'];
        $underRoleCode = $this->getAllRoleUnderCode($role->code);
        $roleList = AdminRoleModel::whereIn("code", $underRoleCode)->where([
            [ 'deleted', "=", 0],
        ])->orderBy('id','asc')->get()->toarray();
        if ($role->ltarCode == "-1")
        {
            $rules = AdminRuleModel::where([
                ['status', '=', 1],
                ['deleted', '=', 0]
            ])->orderBy("sort", "asc")->get()->toArray();
            $rulesAction = AdminRuleActionModel::where([
                ['status', '=', 1],
                ['deleted', '=', 0]
            ])->orderBy("sort", "asc")->get()->toArray();
        }else{
            $ruleCodes = explode(",", $role->ltarCode);
            $rules = AdminRuleModel::whereIn("code", $ruleCodes)->where([
                ['status', '=', 1],
                ['deleted', '=', 0]
            ])->orderBy("sort", "asc")->get()->toArray();
            $ruleActionCodes = explode(",", $role->ltaraCode);
            $rulesAction = AdminRuleModel::whereIn("code", $ruleActionCodes)->where([
                ['status', '=', 1],
                ['deleted', '=', 0]
            ])->orderBy("sort", "asc")->get()->toArray();
        }
        return view($this->viewPath."system.role_table", ["roleList"=>$roleList, "rules"=>$rules, "rulesAction"=>$rulesAction]);
    }

    /**
     * 角色列表数据
     * @param Request $request 请求参数 page 当前页码 limit 每页数量
     * @return array|string
     */
    public function pageTableData(Request $request): array|string
    {
        $loginInfo = $this->getLoginInfo($request);
        $role = $loginInfo['role'];
        $underRoleCode = $this->getAllRoleUnderCode($role->code, true);
        $roleList = AdminRoleModel::whereIn("code", $underRoleCode)->where([
            [ 'deleted', "=", 0],
        ])->orderBy('id','asc')->get()->toarray();
        $treeData = dataToTree($roleList, "code", "pCode");
        return successReturn("加载成功", $treeData);
    }

}
